package com.pand.service;

import com.pand.model.order.Order;
import com.pand.model.order.OrderItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * Created by lh on 2017/11/3.
 */
@Repository("originalOrderService")
public interface OrderService extends JpaRepository<Order, Long> {
    @Query("select o from Order o , OrderItem item " +
            "where o.orderNo = item.orderNo " +
            "and item.realGetTime >= ?1 " +
            "and item.realRetTime <= ?2 " +
            "and (item.realGetTime is not null " +
            "and item.realRetTime is not null)")
    List<Order> findOrdersByRealTime(Date startTime, Date endTime);

    @Query("select o from Order o , OrderItem item " +
            "where o.orderNo = item.orderNo " +
            "and item.getTime >= ?1 " +
            "and item.retTime <= ?2 " +
            "and (item.realGetTime is null or item.realRetTime is null)")
    List<Order> findOrdersByExpectedTime(Date startTime, Date endTime);

    @Query("select item from OrderItem item " +
            "where item.orderNo = ?1")
    OrderItem findOrderItemByOrderNo(String orderNo);

    @Query("select v.name from SystemCode v " +
            "where v.type = '1023' " +
            "and v.code = ?1")
    String getOrderStatusName(String status);

    @Query("select o from Order o , OrderItem item " +
            "where o.orderNo = item.orderNo " +
            "and o.cityCode = '400000'" +
            "and ((o.createTime > ?1 and o.createTime <= ?2)" +
            "or (item.realGetTime > ?1 and item.realGetTime <= ?2)" +
            "or (item.realRetTime > ?1 and item.realRetTime <= ?2))")
    List<Order> findOrdersByTime(Date startTime, Date nowTime);

    Long countByCreateTimeBetween(Date startTime, Date now);

//    @Query("select o from Order o " +
//            "where o.createTime > ?1 " +
//            "and o.createTime <= ?2")
//    List<Order> findByCreateTime(Date startTime, Date nowTime);
//
//    @Query("select o from Order o , OrderItem item " +
//            "where o.orderNo = item.orderNo " +
//            "and item.realGetTime > ?1 " +
//            "and item.realGetTime <= ?2 ")
//    List<Order> findByRealGetTime(Date startTime, Date nowTime);
//
//    @Query("select o from Order o , OrderItem item " +
//            "where o.orderNo = item.orderNo " +
//            "and item.realRetTime > ?1 " +
//            "and item.realRetTime <= ?2 ")
//    List<Order> findByRealRetTime(Date startTime, Date nowTime);
}
